# usr/bin/python

    
def least_sqm(x, y, n):
    avex = least_ave(x, n)
    avey = least_ave(y, n)
    b = (least_sumxy(x, y, n) - n * avex * avey) / (least_sum_xsq(x, n) - n * least_ave(x, n) * least_ave(x, n))
    a = least_ave(y, n) - b * least_ave(x, n)
    res = list()
    res.append(a)
    res.append(b)
    return res

def least_ave(di, n):
    res = 0
    for i in range(n):
        res += di[i]
    res = res / n
    return res

def least_sum_xsq(di, n):
    res = 0
    for i in range(n):
        res += di[i] * di[i]

    return res

def least_sumxy(x, y, n):
    res = 0
    for i in range(n):
        res += x[i] * y[i]
    return res

if __name__ == '__main__':
    x = [155,198,244,291,338,399,437,485,532,582]
    y = [150,200,250,300,350,400,450,500,550,600]
    res = least_sqm(x, y, 4)
    print(res)

